
//
/* 
 @from http://en.wikipedia.org/wiki/Continued_fraction
 
 h(n) = a(n)*h(n-1) + h(n-2)
 k(n) = a(n)*k(n-1) + k(n-2)
 
 and 
 h(0) = a(0), k(0) = 1;
 h(1) = a(0)*a(1) + 1, k(1) = a(1);
*/


//preloading.....
load("library/common.js");

JEuler.system.loadJS("library/math.js");
JEuler.system.loadJS("library/bigInteger.js");

(function() {

    var BigInteger = JEuler.math.BigInteger;

    //JEuler.system.loadJS("data/???.dat");
    JEuler.system.printCaption("65");
    JEuler.system.startTimerCount();
    
    //e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
    function a(n) {
        if(n==0) return 2;
        if(n%3==1||n%3==0) return 1;
        return 2*(n+1)/3;
    }
    
    var h=[], k=[];
    h[0] = new BigInteger(a(0)); 
    k[0] = new BigInteger(1);
    h[1] = new BigInteger(a(0)*a(1) + 1); 
    k[1] = new BigInteger(a(1));
    
    JEuler.system.print("h("+(1)+")=" + h[0]);
    JEuler.system.print("h("+(2)+")=" + h[1]);
    for(var n=2; n<=99; n++) {
        h[n] = h[n-1].clone().times(a(n)).add(h[n-2]);
        k[n] = k[n-1].clone().times(a(n)).add(k[n-2]);
        JEuler.system.print("h("+(n+1)+")=" + h[n]);
        //JEuler.system.print("k("+n+")=" + k[n]);
    }
    
    JEuler.system.print("Sum = " + eval(h[99].toString().split("").join("+")));

    JEuler.system.printTimerCount();
    
})();
